

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 参考资料 SPOC 运行环境  windows10 IDEA 2021.1 专业版 JDK8 Tomcat 9 Spring 5.0.5 MyBatis3  一、SpringMVC之Cntroller层 1.1 重定向语法：在Controller层，redirect:重定向低址 使用案例：退出登陆 12345@RequestMapping(&quot;&#x2F;loginout&quot;)">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringMVC 笔记2 控制层重定向与请求转发 _ 异常处理 _ SSM框架整合案例 _ 分页插件以及使用 _ 后端数据校验机制_ 文件上传问题">
<meta property="og:url" content="http://example.com/2022/01/25/SpringMVC%20%E7%AC%94%E8%AE%B02%20%E6%8E%A7%E5%88%B6%E5%B1%82%E9%87%8D%E5%AE%9A%E5%90%91%E4%B8%8E%E8%AF%B7%E6%B1%82%E8%BD%AC%E5%8F%91%20_%20%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%20_%20SSM%E6%A1%86%E6%9E%B6%E6%95%B4%E5%90%88%E6%A1%88%E4%BE%8B%20_%20%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8%20_%20%E5%90%8E%E7%AB%AF%E6%95%B0%E6%8D%AE%E6%A0%A1%E9%AA%8C%E6%9C%BA%E5%88%B6_%20%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E9%97%AE%E9%A2%98/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 参考资料 SPOC 运行环境  windows10 IDEA 2021.1 专业版 JDK8 Tomcat 9 Spring 5.0.5 MyBatis3  一、SpringMVC之Cntroller层 1.1 重定向语法：在Controller层，redirect:重定向低址 使用案例：退出登陆 12345@RequestMapping(&quot;&#x2F;loginout&quot;)">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/5630d91a66fd4def89d0f0d1e7e2ca70.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/3f4faaf4b8b543859f51763d0401555e.png">
<meta property="article:published_time" content="2022-01-25T15:46:40.000Z">
<meta property="article:modified_time" content="2022-08-22T15:49:02.939Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="SpringMVC">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/5630d91a66fd4def89d0f0d1e7e2ca70.png#pic_center">
  
  
  
  <title>SpringMVC 笔记2 控制层重定向与请求转发 _ 异常处理 _ SSM框架整合案例 _ 分页插件以及使用 _ 后端数据校验机制_ 文件上传问题 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="SpringMVC 笔记2 控制层重定向与请求转发 _ 异常处理 _ SSM框架整合案例 _ 分页插件以及使用 _ 后端数据校验机制_ 文件上传问题"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-01-25 23:46" pubdate>
          2022年1月25日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          19k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          161 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">SpringMVC 笔记2 控制层重定向与请求转发 _ 异常处理 _ SSM框架整合案例 _ 分页插件以及使用 _ 后端数据校验机制_ 文件上传问题</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="http://spoc.wzu.edu.cn/study/initplay/795266.mooc">SPOC</a></p>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>windows10</li>
<li>IDEA 2021.1 专业版</li>
<li>JDK8</li>
<li>Tomcat 9</li>
<li>Spring 5.0.5</li>
<li>MyBatis3</li>
</ul>
<h1 id="一、SpringMVC之Cntroller层"><a href="#一、SpringMVC之Cntroller层" class="headerlink" title="一、SpringMVC之Cntroller层"></a>一、SpringMVC之Cntroller层</h1><hr>
<h2 id="1-1-重定向"><a href="#1-1-重定向" class="headerlink" title="1.1 重定向"></a>1.1 重定向</h2><p>语法：在Controller层，<code>redirect:重定向低址</code></p>
<p>使用案例：退出登陆</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/loginout&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">loginout</span><span class="hljs-params">(HttpSession session)</span>&#123;<br>	session.invalidate();<br>	<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;redirect:/user/login.html&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="1-2-请求转发"><a href="#1-2-请求转发" class="headerlink" title="1.2 请求转发"></a>1.2 请求转发</h2><p>语法：在Controller层，<code>forward:请求转发地址</code>（默认）</p>
<p>使用案例：跳转页面</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/home&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">login</span><span class="hljs-params">(HttpSession session)</span>&#123;<br>	<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;forward:/home.html&quot;</span><br>	<span class="hljs-comment">// 可简写成 return &quot;home.html&quot; </span><br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="1-3-调用HttpSession、HttpServletRequest对象"><a href="#1-3-调用HttpSession、HttpServletRequest对象" class="headerlink" title="1.3 调用HttpSession、HttpServletRequest对象"></a>1.3 调用HttpSession、HttpServletRequest对象</h2><p>该对象是在请求Controller层时自带的，和之前Servlet类里的doGet、doPost的两个参数一样，不过在SpringMVC里这些对象可以省略不写。</p>
<p>使用案例：实现登陆错误提示<br><code>UserAction.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(value = &quot;/dologin&quot;, method = RequestMethod.POST)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">dologin</span><span class="hljs-params">(User user, HttpSession session, HttpServletRequest request)</span>&#123;<br>	<span class="hljs-keyword">if</span>(userService.login(user.getUserCode(), user.getUserPassword()))&#123;<br>		<span class="hljs-comment">// 登陆成功</span><br>		session.setAttribute(<span class="hljs-string">&quot;userInfo&quot;</span>, user);<br>		<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;index&quot;</span><br>	&#125;<br>	<span class="hljs-comment">// 登陆失败</span><br>	request.setAttribute(<span class="hljs-string">&quot;loginErr&quot;</span>, <span class="hljs-string">&quot;用户名密码不匹配&quot;</span>);<br>	<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;login&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<p><code>login.jsp</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;loginForm&quot;</span> <span class="hljs-attr">action</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath&#125;/user/dologin&quot;</span></span><br><span class="hljs-tag">                  <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;actionForm&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;actionForm&quot;</span>  <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> &gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;info&quot;</span>&gt;</span>$&#123;requestScope.loginErr&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;inputbox&quot;</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;userCode&quot;</span>&gt;</span>用户名：<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;input-text&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userCode&quot;</span></span><br><span class="hljs-tag">                        <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;userCode&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;请输入用户名&quot;</span> <span class="hljs-attr">required</span>/&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>	<br>	<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;inputbox&quot;</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;userPassword&quot;</span>&gt;</span>密码：<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userPassword&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;userPassword&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;请输入密码&quot;</span> <span class="hljs-attr">required</span>/&gt;</span><br>             <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>	<br>	<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;subBtn&quot;</span>&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;登录&quot;</span>/&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;reset&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;重置&quot;</span>/&gt;</span><br>             <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>	<br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h1 id="二、SpringMVC异常处理"><a href="#二、SpringMVC异常处理" class="headerlink" title="二、SpringMVC异常处理"></a>二、SpringMVC异常处理</h1><hr>
<h2 id="2-1-全局异常处理"><a href="#2-1-全局异常处理" class="headerlink" title="2.1 全局异常处理"></a>2.1 全局异常处理</h2><ul>
<li>对所有异常进行统一管理</li>
<li>通过配置 <code>SimpleMappingExceptionResolver</code></li>
</ul>
<p>使用案例：在SpringMVC配置文件中指定遇到异常后跳转的页面</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.springframework.web.servlet.handler.SimpleMappingExceptionResolver&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;exceptionMappings&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">props</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;java.lang.RuntimeException&quot;</span>&gt;</span>error<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">props</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>其中<code>&lt;prop key=&quot;java.lang.RuntimeException&quot;&gt;error&lt;/prop&gt;</code>指定了在遇到Exception异常时就跳转逻辑的视图名为error的页面（经过视图解析器ModelAndView后的实际页面）</p>
<p>这里可以看到对于不同的异常，可以设置跳转到不同的页面。</p>
<h2 id="2-2-Controller层跳转全局异常"><a href="#2-2-Controller层跳转全局异常" class="headerlink" title="2.2 Controller层跳转全局异常"></a>2.2 Controller层跳转全局异常</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/globException&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">testGlobleException</span><span class="hljs-params">()</span>&#123;<br>    <span class="hljs-keyword">if</span>(<span class="hljs-literal">true</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(<span class="hljs-string">&quot;超时错误&quot;</span>);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;login&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>这里仅仅做一个测试，对应2.1的部分，设置了RuntimeException异常的跳转页面，这里在访问&#x2F;globException后，能直接跳转到错误的页面<code>error.html</code></p>
<h1 id="三、SSM-框架整合案例"><a href="#三、SSM-框架整合案例" class="headerlink" title="三、SSM 框架整合案例"></a>三、SSM 框架整合案例</h1><hr>
<p>SSM使用案例：获取所有用户列表</p>
<p>路线： DAO -&gt; Service -&gt; Action -&gt; View</p>
<p>思路：所有的类和方法都注入在Spring IoC容器中，MyBatis框架负责DAO层数据库交互，以及Service层实现查询用户的业务需求，SpringMVC框架负责Action层，处理前端向后端发送的请求，在由Action返回的参数渲染View视图，最后返回给前端页面。</p>
<p><strong>DAO层</strong></p>
<p><code>UserMapper.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">UserMapper</span>&#123;<br>	<span class="hljs-keyword">public</span> List&lt;User&gt; <span class="hljs-title function_">getUserList</span><span class="hljs-params">()</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<p><code>UserMapper.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;getUserList&quot;</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">&quot;User&quot;</span>&gt;</span><br>	select * from user<br><span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>Service层</strong><br><code>UserServiceImpl.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> List&lt;User&gt; <span class="hljs-title function_">getUserList</span><span class="hljs-params">()</span> &#123;<br>	<span class="hljs-keyword">return</span> userMapper.getUserList();<br>&#125;<br></code></pre></td></tr></table></figure>
<p><strong>Action层</strong><br><code>UserAction.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/findAll&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">findAllUsers</span><span class="hljs-params">(Model model)</span>&#123;<br>	<span class="hljs-comment">// 通过 Model 渲染视图</span><br>	List&lt;User&gt; users = userService.getUserList();<br>	model.addAttribute(<span class="hljs-string">&quot;users&quot;</span>, users);<br>	<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;user/userlist&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<p><strong>前端页面</strong></p>
<p>跳转 .jsp</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath&#125;/findAll&quot;</span>&gt;</span>用户查询<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>显示 .jsp</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs html">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt;<br><span class="hljs-tag">&lt;<span class="hljs-name">c:forEach</span> <span class="hljs-attr">var</span>=<span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">items</span>=<span class="hljs-string">&quot;$&#123;users &#125;&quot;</span> <span class="hljs-attr">varStatus</span>=<span class="hljs-string">&quot;status&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>$&#123;user.userCode &#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>$&#123;user.userName &#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">c:if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;$&#123;user.gender==1&#125;&quot;</span>&gt;</span>男<span class="hljs-tag">&lt;/<span class="hljs-name">c:if</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">c:if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;$&#123;user.gender==2&#125;&quot;</span>&gt;</span>女<span class="hljs-tag">&lt;/<span class="hljs-name">c:if</span>&gt;</span><br>			<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>$&#123;user.birthday&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>$&#123;user.phone&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>$&#123;user.userRoleName&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;viewUser&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;javascript:;&quot;</span> <span class="hljs-attr">userid</span>=<span class="hljs-string">$&#123;user.id</span> &#125; <span class="hljs-attr">username</span>=<span class="hljs-string">$&#123;user.userName</span> &#125;&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath &#125;/statics/images/read.png&quot;</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">&quot;查看&quot;</span> <span class="hljs-attr">title</span>=<span class="hljs-string">&quot;查看&quot;</span>/&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;modifyUser&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;javascript:;&quot;</span> <span class="hljs-attr">userid</span>=<span class="hljs-string">$&#123;user.id</span> &#125; <span class="hljs-attr">username</span>=<span class="hljs-string">$&#123;user.userName</span> &#125;&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath &#125;/statics/images/xiugai.png&quot;</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">&quot;修改&quot;</span> <span class="hljs-attr">title</span>=<span class="hljs-string">&quot;修改&quot;</span>/&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;deleteUser&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;javascript:;&quot;</span> <span class="hljs-attr">userid</span>=<span class="hljs-string">$&#123;user.id</span> &#125; <span class="hljs-attr">username</span>=<span class="hljs-string">$&#123;user.userName</span> &#125;&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath &#125;/statics/images/schu.png&quot;</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">&quot;删除&quot;</span> <span class="hljs-attr">title</span>=<span class="hljs-string">&quot;删除&quot;</span>/&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">c:forEach</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>在整合SSM框架时，配置文件尤为重要，以当前这个案例为例：</p>
<h2 id="3-1-SSM整合案例——Tomcat的配置"><a href="#3-1-SSM整合案例——Tomcat的配置" class="headerlink" title="3.1 SSM整合案例——Tomcat的配置"></a>3.1 SSM整合案例——Tomcat的配置</h2><p><strong><code>web.xml</code> Tomcat的配置</strong></p>
<p>包括以下几个内容：<br>（1）指定Spring配置文件、设置Spring监听器，用于初始化容器，它可以自动装配ApplicationContext的配置信息<br>（2）设置中文乱码过滤，使用filter过滤器配置Spring框架提供的类将所有页面设置成UTF-8的编码格式<br>（3）使用servlet标签定义SpringMVC的前端控制器，即DispatcherServlet类，并配置SpringMVC的配置文件的路径</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">version</span>=<span class="hljs-string">&quot;4.0&quot;</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- 指定Spring配置文件 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">context-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>contextConfigLocation<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>classpath:applicationContext.xml<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">context-param</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 初始化Spring容器 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">listener</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">listener-class</span>&gt;</span>org.springframework.web.context.ContextLoaderListener<span class="hljs-tag">&lt;/<span class="hljs-name">listener-class</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">listener</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">filter</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>encodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">filter-class</span>&gt;</span><br>            org.springframework.web.filter.CharacterEncodingFilter<br>        <span class="hljs-tag">&lt;/<span class="hljs-name">filter-class</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>encoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>forceEncoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">filter-mapping</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>encodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/*<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">filter-mapping</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>dispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>contextConfigLocation<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>classpath:springmvc.xml<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>dispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="3-2-SSM整合案例——Spring的配置"><a href="#3-2-SSM整合案例——Spring的配置" class="headerlink" title="3.2 SSM整合案例——Spring的配置"></a>3.2 SSM整合案例——Spring的配置</h2><p><code>applicationContext.xml</code> Spring 的配置<br>（1）使用context标签指定，扫描带Spring注解的包，这样就可以通过注解注入类、方法、变量到Spring容器，或者是从Spring容器里获取某个Bean赋值给变量，比如@Autowired注解<br>（2）使用context标签加载数据库的配置文件，使用c3p0数据源的类注入一个dataSource的Bean<br>（3）使用dataSource的Bean在根据MyBatis框架提供的SqlSessionFactoryBean类注入sqlSessionFactoryBean，注入时，需指定MyBatis的配置文件路径和mapper的SQL映射文件<br>（4）在Spring容器有了sqlSessionFactoryBean后，再根据MyBatis框架提供的MapperScannerConfigurer类，注入一个Bean，指定需扫描的包，它可以扫描指定包下的Mapper接口类，并自动注入到Spring容器中</p>
<p>通过以上的配置，在Service层定义一个Mapper接口类时使用@Autowired注解，就可以直接调用到mapper的XML映射文件里的语句</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/context&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans</span></span><br><span class="hljs-string"><span class="hljs-tag">	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd</span></span><br><span class="hljs-string"><span class="hljs-tag">    http://www.springframework.org/schema/context</span></span><br><span class="hljs-string"><span class="hljs-tag">    http://www.springframework.org/schema/context/spring-context-4.3.xsd&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.uni.service&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">context:property-placeholder</span> <span class="hljs-attr">location</span>=<span class="hljs-string">&quot;classpath:database.properties&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;dataSource&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;driverClass&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.driver&#125;&quot;</span> /&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;jdbcUrl&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.url&#125;&quot;</span> /&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.username&#125;&quot;</span> /&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.password&#125;&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!--创建出SqlSessionFactory对象  --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;sqlSessionFactoryBean&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;dataSource&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;dataSource&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- configLocation指定全局配置文件的位置 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;configLocation&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;classpath:mybatis-config.xml&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--mapperLocations: 指定mapper文件的位置, dao包下任意层级子包中任意名称的xml文件--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;mapperLocations&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;classpath:com/uni/dao/**/*.xml&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- DAO接口所在包名，Spring会自动扫描包下的Mapper接口，生成动态代理  --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;basePackage&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;com.uni.dao&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="3-3-SSM整合案例——SpringMVC的配置"><a href="#3-3-SSM整合案例——SpringMVC的配置" class="headerlink" title="3.3 SSM整合案例——SpringMVC的配置"></a>3.3 SSM整合案例——SpringMVC的配置</h2><p><code>springmvc.xml</code> <strong>SpringMVC的配置</strong></p>
<p>对于本次案例，SpringMVC主要是在Action层处理请求，并渲染视图给前端页面</p>
<p>（1）使用<code>&lt;context:component-scan base-package=“包名”</code> 指定支持SpringMVC相关注解比如@Controller、@RequestMapping等的包<br>（2）通过<code>&lt;mvc:annotation-driven/&gt;</code>指定注解驱动，以使得访问路径与方法的匹配可以通过注解配置<br>（3）通过注入<code>InternalResourceViewResolver类</code>指定视图解析器，可以指定<code>suffix</code>和<code>prefix</code>两个属性，这样在Controller层的方法返回String类型的值时，会自动拼接这个prefix前缀和suffix后缀</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="hljs-tag">	<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">	<span class="hljs-attr">xmlns:mvc</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/mvc&quot;</span></span><br><span class="hljs-tag">	<span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/context&quot;</span></span><br><span class="hljs-tag">	<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans </span></span><br><span class="hljs-string"><span class="hljs-tag">	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd</span></span><br><span class="hljs-string"><span class="hljs-tag">		http://www.springframework.org/schema/context </span></span><br><span class="hljs-string"><span class="hljs-tag">		http://www.springframework.org/schema/context/spring-context-4.3.xsd</span></span><br><span class="hljs-string"><span class="hljs-tag">		http://www.springframework.org/schema/mvc</span></span><br><span class="hljs-string"><span class="hljs-tag">        http://www.springframework.org/schema/mvc/spring-mvc.xsd&quot;</span>&gt;</span><br><br>	<span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.uni.action&quot;</span>/&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">mvc:annotation-driven</span>/&gt;</span><br><br>	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;modelAndView&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;prefix&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;/WEB-INF/jsp/&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;suffix&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;.jsp&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>	<br><span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>				<br></code></pre></td></tr></table></figure>

<h1 id="四、使用分页插件"><a href="#四、使用分页插件" class="headerlink" title="四、使用分页插件"></a>四、使用分页插件</h1><hr>
<p>分页插件帮助文档：<a target="_blank" rel="noopener" href="https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md">https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md</a></p>
<h2 id="4-1-在项目中导入分页插件的jar包，或者依赖"><a href="#4-1-在项目中导入分页插件的jar包，或者依赖" class="headerlink" title="4.1 在项目中导入分页插件的jar包，或者依赖"></a>4.1 在项目中导入分页插件的jar包，或者依赖</h2><p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.github.pagehelper<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>pagehelper<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>最新版本<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="4-2-在Spring配置中添加插件"><a href="#4-2-在Spring配置中添加插件" class="headerlink" title="4.2 在Spring配置中添加插件"></a>4.2 在Spring配置中添加插件</h2><p>这个插件是基于MyBatis框架进行开发的，所以在配置时，根据官方文档的提示，需要在注入SqlSessionFactoryBean时进行配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--创建出SqlSessionFactory对象  --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;sqlSessionFactoryBean&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;dataSource&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;dataSource&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>     <span class="hljs-comment">&lt;!-- configLocation指定全局配置文件的位置 --&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;configLocation&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;classpath:mybatis-config.xml&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>     <span class="hljs-comment">&lt;!--mapperLocations: 指定mapper文件的位置, dao包下任意层级子包中任意名称的xml文件--&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;mapperLocations&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;classpath:com/uni/dao/**/*.xml&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><br>     <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;plugins&quot;</span>&gt;</span><br>         <span class="hljs-tag">&lt;<span class="hljs-name">array</span>&gt;</span><br>             <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.github.pagehelper.PageInterceptor&quot;</span>&gt;</span><br>                 <span class="hljs-comment">&lt;!-- 这里的几个配置主要演示如何使用，如果不理解，一定要去掉下面的配置 --&gt;</span><br>                 <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;properties&quot;</span>&gt;</span><br>                     <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span><br>                         helperDialect=mysql<br>                         supportMethodsArguments=true<br>                         params=count=countSql<br>                         autoRuntimeDialect=true<br>                     <span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>                 <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>             <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>         <span class="hljs-tag">&lt;/<span class="hljs-name">array</span>&gt;</span><br>     <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br> <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h2 id="4-3-分页插件的使用"><a href="#4-3-分页插件的使用" class="headerlink" title="4.3 分页插件的使用"></a>4.3 分页插件的使用</h2><p>控制层</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/findAllByPage&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">findAllUsersByPage</span><span class="hljs-params">(Model model,</span><br><span class="hljs-params">                                 <span class="hljs-meta">@RequestParam(required=true,defaultValue=&quot;1&quot;)</span> Integer pageNow,</span><br><span class="hljs-params">                                 <span class="hljs-meta">@RequestParam(required=false,defaultValue=&quot;5&quot;)</span> Integer pageSize)</span>&#123;<br>    PageHelper.startPage(pageNow, pageSize);<br>    List&lt;User&gt; users = userService.getUserList();<br>    PageInfo&lt;User&gt; pageInfo = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PageInfo</span>&lt;User&gt;(users);<br>    model.addAttribute(<span class="hljs-string">&quot;users&quot;</span>, users);<br>    model.addAttribute(<span class="hljs-string">&quot;pageInfo&quot;</span>, pageInfo);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;user/userlist&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>前端页面</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;hidden&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;totalPageCount&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;pageInfo.pages&#125;&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath&#125;/user/findAllByPage?pageNow=$&#123;pageInfo.prePage&#125;&quot;</span>&gt;</span>上一页<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath&#125;/user/findAllByPage?pageNow=$&#123;pageInfo.nextPage&#125;&quot;</span>&gt;</span>下一页<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>共$&#123;pageInfo.pages&#125;页<br></code></pre></td></tr></table></figure>

<h1 id="五、后台数据校验机制——JSR303"><a href="#五、后台数据校验机制——JSR303" class="headerlink" title="五、后台数据校验机制——JSR303"></a>五、后台数据校验机制——JSR303</h1><hr>
<h2 id="5-1-说明"><a href="#5-1-说明" class="headerlink" title="5.1 说明"></a>5.1 说明</h2><p>通常是前端通过JS对数据进行校验，为了保证网站安全，后台也需要检验其合法性。JSR303通过在Bean的属性上标注类，比如@NotNull、@Max等标准的注解指定校验规则，并通过标准的验证接口对Bean进行验证</p>
<p>后台验证的步骤</p>
<ol>
<li>JavaBean中添加相关的验证注解</li>
<li>action层中在方法的参数里使用@Valid表示要验证的JavaBean，再用BindingResult对象判断是否验证失败</li>
</ol>
<h2 id="5-2-使用案例"><a href="#5-2-使用案例" class="headerlink" title="5.2 使用案例"></a>5.2 使用案例</h2><p>（1）在pojo包下的类对应属性添加限制的注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.pojo;<br><br><span class="hljs-keyword">import</span> org.hibernate.validator.constraints.NotEmpty;<br><span class="hljs-keyword">import</span> org.hibernate.validator.constraints.Range;<br><span class="hljs-keyword">import</span> org.springframework.format.annotation.DateTimeFormat;<br><br><span class="hljs-keyword">import</span> javax.validation.constraints.Pattern;<br><span class="hljs-keyword">import</span> java.util.Date;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br>	<span class="hljs-comment">// @Range(max = 100 , min = 1 , message = &quot;ID范围：1 ~ 100&quot;)</span><br>	<span class="hljs-keyword">private</span> Integer id; <span class="hljs-comment">//id </span><br>	<span class="hljs-keyword">private</span> String userCode; <span class="hljs-comment">//用户编码</span><br>	<span class="hljs-meta">@NotEmpty(message = &quot;请输入用户名&quot;)</span><br>	<span class="hljs-keyword">private</span> String userName; <span class="hljs-comment">//用户名称</span><br>	<span class="hljs-keyword">private</span> String userPassword; <span class="hljs-comment">//用户密码</span><br>	<span class="hljs-keyword">private</span> Integer gender;  <span class="hljs-comment">//性别</span><br>	<span class="hljs-meta">@DateTimeFormat(pattern = &quot;yyyy-MM-dd&quot;)</span><br>	<span class="hljs-keyword">private</span> Date birthday;  <span class="hljs-comment">//出生日期</span><br>	<span class="hljs-keyword">private</span> String phone;   <span class="hljs-comment">//电话</span><br>	<span class="hljs-meta">@Pattern(regexp = &quot;\\w+(\\.\\w)*@\\w+(\\.\\w&#123;2,3&#125;)&#123;1,3&#125;&quot; , message = &quot;非法邮箱格式&quot;)</span><br>	<span class="hljs-keyword">private</span> String address; <span class="hljs-comment">//地址</span><br>	<span class="hljs-keyword">private</span> Integer userRole;    <span class="hljs-comment">//用户角色</span><br>	<span class="hljs-keyword">private</span> Integer createdBy;   <span class="hljs-comment">//创建者</span><br>	<span class="hljs-keyword">private</span> Date creationDate; <span class="hljs-comment">//创建时间</span><br>	<span class="hljs-keyword">private</span> Integer modifyBy;     <span class="hljs-comment">//更新者</span><br>	<span class="hljs-keyword">private</span> Date modifyDate;   <span class="hljs-comment">//更新时间</span><br>	<span class="hljs-keyword">private</span> String userRoleName;<br>	<span class="hljs-comment">// getter、setter、toString</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p>（2）在Controller层的方法里设置验证</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(value = &quot;/validate&quot;, method=RequestMethod.POST)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">validateJSR</span><span class="hljs-params">(<span class="hljs-meta">@Valid</span> User user, BindingResult errors)</span>&#123;<br>    <span class="hljs-keyword">if</span> (errors.hasErrors()) &#123;    <span class="hljs-comment">// 如果有一项验证失败，hasErrors就返回true</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;/user/useraddJSR&quot;</span>;<br>    &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;index&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>（3）前端JSP页面显示错误的提示</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs html">&lt;%@taglib uri=&quot;http://www.springframework.org/tags/form&quot; prefix=&quot;form&quot;%&gt;<br><span class="hljs-tag">&lt;<span class="hljs-name">form:errors</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;user.userName&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">form:errors</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">form:errors</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;user.birthday&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">form:errors</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">form:errors</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;user.address&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">form:errors</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h1 id="六、SpringMVC-文件上传"><a href="#六、SpringMVC-文件上传" class="headerlink" title="六、SpringMVC 文件上传"></a>六、SpringMVC 文件上传</h1><hr>
<h2 id="6-1-单个文件上传"><a href="#6-1-单个文件上传" class="headerlink" title="6.1 单个文件上传"></a>6.1 单个文件上传</h2><p>（1）导入依赖<br><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>commons-fileupload<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>commons-fileupload<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.3.3<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- https://mvnrepository.com/artifact/commons-io/commons-io --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>commons-io<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>commons-io<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.8.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br></code></pre></td></tr></table></figure>
<p>（2）前端设置支持文件上传的表单</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath&#125;/user/upload&quot;</span> <span class="hljs-attr">enctype</span>=<span class="hljs-string">&quot;multipart/form-data&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>请选择头像:<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;file&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;file&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;上传&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>（3）Controller层响应请求</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/upload&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">upload</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam(&quot;file&quot;)</span> MultipartFile file, HttpServletRequest request)</span> <span class="hljs-keyword">throws</span> Exception&#123;<br>    <span class="hljs-keyword">if</span>(!file.isEmpty())&#123;<br>        <span class="hljs-comment">//上传文件路径</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">path</span> <span class="hljs-operator">=</span> request.getServletContext().getRealPath(<span class="hljs-string">&quot;/imgs&quot;</span>);<br>        System.out.println(path);<br>        <span class="hljs-comment">//上传文件名</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">filename</span> <span class="hljs-operator">=</span> file.getOriginalFilename();<br>        <span class="hljs-comment">//将上传文件保存到一个目标文件当中</span><br>        file.transferTo(<span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(path + File.separator + filename));<br>    &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>那么服务端保存的图片会在哪里呢？笔者使用的是IDEA2021.1 专业版本，部署Tomcat后，保存在了工件的文件夹下<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/596229e343c54918a3cf79b23bf339c5.png#pic_center#">https://img-blog.csdnimg.cn/596229e343c54918a3cf79b23bf339c5.png#pic_center#</a> &#x3D;60%x)</p>
<h2 id="6-2-多文件上传"><a href="#6-2-多文件上传" class="headerlink" title="6.2 多文件上传"></a>6.2 多文件上传</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">&quot;$&#123;pageContext.request.contextPath&#125;/user/upload2&quot;</span> <span class="hljs-attr">enctype</span>=<span class="hljs-string">&quot;multipart/form-data&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>文    件1<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;file&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;files&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>文    件2<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;file&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;files&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>文    件3<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;file&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;files&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;提交&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/upload2&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">upload2</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam(&quot;files&quot;)</span> MultipartFile[] files, User user</span><br><span class="hljs-params">        , HttpServletRequest request)</span> <span class="hljs-keyword">throws</span> Exception&#123;<br><br>    System.out.println(files);<br>    <span class="hljs-type">String</span> <span class="hljs-variable">path</span> <span class="hljs-operator">=</span> request.getServletContext().getRealPath(<span class="hljs-string">&quot;/imgs&quot;</span>);<br>    System.out.println(path);<br>    <span class="hljs-keyword">for</span>(MultipartFile file : files) &#123;<br>        <span class="hljs-keyword">if</span>(!file.isEmpty()) &#123;<br>            <span class="hljs-type">String</span> <span class="hljs-variable">filename</span> <span class="hljs-operator">=</span> file.getOriginalFilename();<br>            <span class="hljs-comment">//将上传文件保存到一个目标文件当中</span><br>            file.transferTo(<span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(path + File.separator + filename));<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-3-文件上传的限制"><a href="#6-3-文件上传的限制" class="headerlink" title="6.3 文件上传的限制"></a>6.3 文件上传的限制</h2><p>在SpringMVC配置文件中，添加文件管理器，可以拦截不符合要求的文件请求</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;multipartResolver&quot;</span></span><br><span class="hljs-tag">	  <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.springframework.web.multipart.commons.CommonsMultipartResolver&quot;</span>&gt;</span><br>	<span class="hljs-comment">&lt;!-- 上传文件大小上限，单位为字节（10MB） --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;maxUploadSize&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>10485760<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>	<span class="hljs-comment">&lt;!-- 请求的编码格式，必须和jSP的pageEncoding属性一致，以便正确读取表单的内容，默认为ISO-8859-1 --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;defaultEncoding&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="6-4-前端图片显示"><a href="#6-4-前端图片显示" class="headerlink" title="6.4 前端图片显示"></a>6.4 前端图片显示</h2><p>SpringMVC控制层保存的图片是在out文件夹，即工件的文件夹里，而web的页面在指定图片地址时是根据web所在的文件夹为根目录的，现在可以配置一下项目，使得out里存放图片的文件夹同样映射到根目录，这样任何页面都可以通过根目录来显示图片了，而不用做多余的路径修改。</p>
<p>在项目的Tomcat配置中配置工件Artifact部分再添加项目输出的，即out文件夹里的imgs文件夹，这样就可以直接通过根目录显示了。<br><img src="https://img-blog.csdnimg.cn/5630d91a66fd4def89d0f0d1e7e2ca70.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>配置过后，下图中的两个文件夹里的图片，都可以通过<code>/imgs/...</code>来设置图片，其它类型的文件同理。<br><img src="https://img-blog.csdnimg.cn/3f4faaf4b8b543859f51763d0401555e.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/SpringMVC/" class="category-chain-item">SpringMVC</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/SpringMVC/">#SpringMVC</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>SpringMVC 笔记2 控制层重定向与请求转发 _ 异常处理 _ SSM框架整合案例 _ 分页插件以及使用 _ 后端数据校验机制_ 文件上传问题</div>
      <div>http://example.com/2022/01/25/SpringMVC 笔记2 控制层重定向与请求转发 _ 异常处理 _ SSM框架整合案例 _ 分页插件以及使用 _ 后端数据校验机制_ 文件上传问题/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年1月25日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/27/Maven%20%E7%AC%94%E8%AE%B01%20%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA%20_%20%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5%20_%20GAV%E5%9D%90%E6%A0%87%20_%20%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86/" title="Maven 笔记1 项目构建 _ 相关概念 _ GAV坐标 _ 依赖管理">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Maven 笔记1 项目构建 _ 相关概念 _ GAV坐标 _ 依赖管理</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/23/SpringMVC%20%E7%AC%94%E8%AE%B01%20%E6%A1%86%E6%9E%B6%E7%89%B9%E7%82%B9%20_%20%E4%BD%93%E7%B3%BB%E6%9E%B6%E6%9E%84%20_%20%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B%20_%20%E5%8F%82%E6%95%B0%E5%A4%84%E7%90%86%E3%80%81%E4%BC%A0%E9%80%92/" title="SpringMVC 笔记1 框架特点 体系架构 执行流程 参数处理 传递">
                        <span class="hidden-mobile">SpringMVC 笔记1 框架特点 体系架构 执行流程 参数处理 传递</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
